package com.edu.system.service.analysis;

/**
 * @author huangc
 *
 */

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.edu.system.domain.TStudyScore;
import com.edu.system.domain.vo.CountStudentExamVO;

public enum StudentTagKeys {
	TOP_STUDENT("top_student", "尖子生", "top", 0) {
		@Override
		@SuppressWarnings("unchecked")
		public Map<String, StudentTagKeys> invoke(Map<String, Object> context) {
			List<TStudyScore> cores = (List<TStudyScore>) context.get("studentScoreDetails");
			List<CountStudentExamVO> nums = (List<CountStudentExamVO>) context.get("honorsClassExameStudentNums");
			List<TStudyScore> newCores = cores.stream().filter(num -> "总分".equals(num.getLessonName()))
					.filter(num -> num.getTitle().matches(".*(期中|期末).*")).collect(Collectors.toList());

			Map<String, Integer> examStuNumMap = new HashMap<String, Integer>();
			for (CountStudentExamVO row : nums) {
				if (!"总分".equals(row.getLessonName()))
					continue;
				if (examStuNumMap.containsKey(row.getExamId())) {
					examStuNumMap.put(row.getExamId(), Math.max(examStuNumMap.get(row.getExamId()), row.getStuNum()));
				} else {
					examStuNumMap.put(row.getExamId(), row.getStuNum());
				}
			}
			Map<String, StudentTagKeys> result = new HashMap<String, StudentTagKeys>();
			for(TStudyScore row : newCores) {
				int totalStuNum = examStuNumMap.get(String.valueOf(row.getExamId()));
				if (row.getRankSchool() < totalStuNum * 0.15) {
					result.put(row.getStuNo(), StudentTagKeys.TOP_STUDENT);
				}
			}
			return result;
		}
	},
	TOTAL_SCORE_TOP1("total_score_top1", "总分第一名", "totalscore", 0) {

		@Override
		@SuppressWarnings("unchecked")
		public Map<String, StudentTagKeys> invoke(Map<String, Object> context) {
			List<TStudyScore> cores = (List<TStudyScore>) context.get("studentScoreDetails");
			Map<String, StudentTagKeys> result = cores.stream().filter(num -> "总分".equals(num.getLessonName()))
					.filter(num -> num.getTitle().matches(".*(期中|期末).*")).filter(num -> num.getRankSchool() == 1)
					.collect(Collectors.toMap(TStudyScore::getStuNo, s -> StudentTagKeys.TOTAL_SCORE_TOP1 , (k1, k2) -> k1) ) ;
			return result;
		}
	},

	TOTAL_SCORE_TOP10("total_score_top10", "总分前十名", "totalscore", 1) {
		@Override
		@SuppressWarnings("unchecked")
		public Map<String, StudentTagKeys> invoke(Map<String, Object> context) {
			List<TStudyScore> cores = (List<TStudyScore>) context.get("studentScoreDetails");
			Map<String, StudentTagKeys> result = cores.stream().filter(num -> "总分".equals(num.getLessonName()))
					.filter(num -> num.getTitle().matches(".*(期中|期末).*")).filter(num -> num.getRankSchool() <= 10)
					.collect(Collectors.toMap(TStudyScore::getStuNo, s -> StudentTagKeys.TOTAL_SCORE_TOP10, (k1, k2) -> k1));

			return result;
		}
	},
	TOTAL_SCORE_TOP10_PER("total_score_top10_per", "总分前10%", "totalscore", 2) {
		@Override
		@SuppressWarnings("unchecked")
		public Map<String, StudentTagKeys> invoke(Map<String, Object> context) {
			List<TStudyScore> cores = (List<TStudyScore>) context.get("studentScoreDetails");
			List<CountStudentExamVO> nums = (List<CountStudentExamVO>) context.get("honorsClassExameStudentNums");
			List<TStudyScore> newCores = cores.stream().filter(num -> "总分".equals(num.getLessonName()))
					.filter(num -> num.getTitle().matches(".*(期中|期末).*")).collect(Collectors.toList());

			Map<String, Integer> examStuNumMap = new HashMap<String, Integer>();
			for (CountStudentExamVO row : nums) {
				if (!"总分".equals(row.getLessonName()))
					continue;
				if (examStuNumMap.containsKey(row.getExamId())) {
					examStuNumMap.put(row.getExamId(), Math.max(examStuNumMap.get(row.getExamId()), row.getStuNum()));
				} else {
					examStuNumMap.put(row.getExamId(), row.getStuNum());
				}
			}
			Map<String, StudentTagKeys> result = new HashMap<String, StudentTagKeys>();
			for(TStudyScore row : newCores) {
				int totalStuNum = examStuNumMap.get(String.valueOf(row.getExamId()));
				if (row.getRankSchool() < totalStuNum * 0.1) {
					result.put(row.getStuNo(), StudentTagKeys.TOTAL_SCORE_TOP10_PER);
				}
			}
			return result;
		}
	},
	TOTAL_SCORE_TOP20_PER("total_score_top20_per", "总分前20%", "totalscore", 3) {
		@Override
		@SuppressWarnings("unchecked")
		public Map<String, StudentTagKeys> invoke(Map<String, Object> context) {
			List<TStudyScore> cores = (List<TStudyScore>) context.get("studentScoreDetails");
			List<CountStudentExamVO> nums = (List<CountStudentExamVO>) context.get("honorsClassExameStudentNums");
			List<TStudyScore> newCores = cores.stream().filter(num -> "总分".equals(num.getLessonName()))
					.filter(num -> num.getTitle().matches(".*(期中|期末).*")).collect(Collectors.toList());

			Map<String, Integer> examStuNumMap = new HashMap<String, Integer>();
			for (CountStudentExamVO row : nums) {
				if (!"总分".equals(row.getLessonName()))
					continue;
				if (examStuNumMap.containsKey(row.getExamId())) {
					examStuNumMap.put(row.getExamId(), Math.max(examStuNumMap.get(row.getExamId()), row.getStuNum()));
				} else {
					examStuNumMap.put(row.getExamId(), row.getStuNum());
				}
			}
			Map<String, StudentTagKeys> result = new HashMap<String, StudentTagKeys>();
			for(TStudyScore row : newCores) {
				int totalStuNum = examStuNumMap.get(String.valueOf(row.getExamId()));
				if (row.getRankSchool() < totalStuNum * 0.2) {
					result.put(row.getStuNo(), StudentTagKeys.TOTAL_SCORE_TOP20_PER);
				}
			}
			return result;
		}
	},;

	private final String key;
	private final String name;
	private final String group;
	private final int priority;

	StudentTagKeys(String key, String name, String group, int priority) {
		this.key = key;
		this.name = name;
		this.group = group;
		this.priority = priority;
	}

	public String getKey() {
		return key;
	}

	public String getName() {
		return name;
	}

	public String getGroup() {
		return group;
	}

	public int getPriority() {
		return priority;
	}

	public abstract Map<String, StudentTagKeys> invoke(Map<String, Object> context);

}
